<?php

require_once('persistence/AbstractGenericDao.class.php');
require_once('persistence/IVoteDao.class.php');

DEFINE('_SQL_INSERT_VOTE', 'INSERT INTO ' . TBL_VOTE . ' (user_id, topic_id, created) VALUES (:user_id, :topic_id, :created)');

DEFINE('_SQL_DELETE_VOTE', 'DELETE FROM ' . TBL_VOTE . ' WHERE user_id=:user_id AND topic_id=:topic_id');


class GenericVoteDao extends AbstractGenericDao implements IVoteDao {

	public function GenericVoteDao($db) {
		parent::__construct($db);
	}
	
	public function insertVote($userId, $topicId, $created) {
		Log::instance()->debug("Invoking insertVote(userId=$userId;topicId=$topicId)");
		$stmt = $this->prepareStatement(_SQL_INSERT_VOTE);
		
		$stmt->bindParam(':user_id', $userId, PDO::PARAM_INT);
		$stmt->bindParam(':topic_id', $topicId, PDO::PARAM_INT);
		$stmt->bindParam(':created', unixToMySQL($created), PDO::PARAM_STR);
		
		$stmt->execute();
	}
	
	public function deleteVote($userId, $topicId) {
		Log::instance()->debug("Invoking deleteVote(userId=$userId;topicId=$topicId)");
		$stmt = $this->prepareStatement(_SQL_DELETE_VOTE);
		
		$stmt->bindParam(':user_id', $userId, PDO::PARAM_INT);
		$stmt->bindParam(':topic_id', $topicId, PDO::PARAM_INT);
		
		// TODO check affected rows == 1
		$stmt->execute();
	}
	
}

?>